package com.zqq.base.utils

import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

class StringUtils {
    companion object {

        private const val reg_phone = "^1[3456789]\\d{9}$"
        private const val reg_password = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$"

        private const val reg_username = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{1,}$"

        private const val reg_car_no = "^[a-zA-Z0-9]{5,6}$"

        private const val reg_id = "^\\d{17}[xX0-9]$"

        private const val charCheck =
            "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

        private const val reg_money = "^\\d{1,3}(\\.)?(\\d{1,2})?$"  //11111.11

        fun checkChar(str: String): Int {
            if (str.isNullOrEmpty()) {
                return -1
            }

            str.forEachIndexed { index, c ->
                if (charCheck.indexOf(c) < 0) {
                    return index
                }
            }
            return -1
        }


        fun checkUsername(username: String?): Boolean {
            if (username.isNullOrEmpty()) {
                return false
            }
            return username!!.matches(Regex(reg_username))
        }

        fun checkPhone(phone: String?): Boolean {
            if (phone.isNullOrEmpty()) {
                return false
            }
            return phone!!.matches(Regex(reg_phone))
        }

        fun checkCardId(cardId: String?): Boolean {
            if (cardId.isNullOrEmpty()) {
                return false
            }
            return cardId!!.matches(Regex(reg_id))
        }

        fun checkCarNo(carNo: String?): Boolean {
            if (carNo.isNullOrEmpty()) {
                return false
            }
            return carNo!!.matches(Regex(reg_car_no))
        }

        fun checkPassword(paw: String?): Boolean {
            if (paw.isNullOrEmpty()) {
                return false
            }
            return paw!!.matches(Regex(reg_password))
        }

        fun checkMoney(money: String?): Boolean {
            if (money.isNullOrEmpty()) {
                return false
            }
            return money!!.matches(Regex(reg_money)) || money!!.matches(Regex(reg_money))
        }

        fun getTime(duration: Int): String {
            var min = duration / 60  //分钟
            val sec = duration % 60  //秒
            if (min == 0) {
                return "${sec}秒"
            }
            var hour = min / 60  //时
            min = min % 60 //分
            if (hour == 0) {
                val s = if (sec < 10) {
                    "0${sec}"
                } else {
                    "${sec}"
                }
                return "${min}分${s}秒"
            }

            val m = if (min < 10) {
                "0${min}"
            } else {
                "${min}"
            }

            val s = if (sec < 10) {
                "0${sec}"
            } else {
                "${sec}"
            }

            return "${hour}时${m}分${s}秒"
        }

        fun getMD5(input: String): String? {
            try {
                // Create MD5 Hash
                val digest = MessageDigest.getInstance("MD5")
                digest.update(input.toByteArray())
                val messageDigest = digest.digest()

                // Create Hex String
                val hexString = StringBuilder()
                for (aMessageDigest in messageDigest) {
                    var h = Integer.toHexString(0xFF and aMessageDigest.toInt())
                    while (h.length < 2) h = "0$h"
                    hexString.append(h)
                }
                return hexString.toString()
            } catch (e: NoSuchAlgorithmException) {
                e.printStackTrace()
            }
            return ""
        }
    }
}